const fs = require('fs');
const path = require('path');

module.exports = async (forgeConfig) => {
    // 定义要注入的环境变量
    const envVariables = {
        API_URL: 'https://example.com/api',
        DEBUG_MODE: true
    };

    // 获取主进程文件的路径（假设为 main.js）
    const mainFilePath = path.join(forgeConfig.buildIdentifier, 'main.js');

    // 读取主进程文件的内容
    const mainFileContent = fs.readFileSync(mainFilePath, 'utf8');

    // 生成用于设置全局环境变量的代码字符串
    let envVariablesCode = 'global.process.env = {...global.process.env, ';
    for (const key in envVariables) {
        const value = typeof envVariables[key] === 'string'? `'${envVariables[key]}'` : envVariables[key];
        envVariablesCode += `${key}: ${value}, `;
    }
    envVariablesCode = envVariablesCode.slice(0, -2) + ' };';

    // 将设置环境变量的代码插入到主进程文件的顶部
    const updatedMainFileContent = envVariablesCode + '\n' + mainFileContent;

    // 将更新后的内容写回主进程文件
    fs.writeFileSync(mainFilePath, updatedMainFileContent);

    return forgeConfig;
};